#include <cv.h>
#include <highgui.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<dirent.h>
#include<stdlib.h>

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "Core.h"

void cannyEdge(const char* input, const char* output)
{
	IplImage* pImg = NULL; 
	IplImage* pCannyImg = NULL;

	if((pImg = cvLoadImage(input,CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
		printf("Can't load %s\n",input);
		exit(1);
	}
	pCannyImg = cvCreateImage(cvGetSize(pImg),
			IPL_DEPTH_8U,1);
	cvCanny(pImg, pCannyImg, 50, 150, 3);

	cvSaveImage(output,pCannyImg);

	cvReleaseImage(&pImg);
	cvReleaseImage(&pCannyImg);
}

int main( int argc, char** argv )
{
	if(argc != 4) {
		printf("Usuage: ./exe input_image_dir output_edge_dir first_file_id\n");
		exit(1);
	}
	
	struct dirent *dptr;
	DIR *dirp;
	
	if((dirp=opendir(argv[1]))==NULL) {
		printf("can't open directory %s\n",argv[1]);
		exit(1);
	}
	
	int outputEdgeID = atoi(argv[3]);
	char buf[1024] = {0}, buf2[1024] = {0};
	while(dptr=readdir(dirp)) {
		if(strstr(dptr->d_name,".jpg") == 0)
			continue;
		sprintf(buf,"%s%s",argv[1],dptr->d_name);
		sprintf(buf2,"%s%05d.jpg",argv[2],outputEdgeID);
		outputEdgeID++;
		cannyEdge(buf,buf2);
	}

	closedir(dirp);
	return 0;
	
}
